package com.jinyi.wwdeng.ui.privacypolicy

import android.annotation.SuppressLint
import android.view.Gravity
import android.view.View
import android.webkit.WebChromeClient
import android.webkit.WebResourceRequest
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import com.jinyi.wwdeng.AppConfigs
import com.jinyi.wwdeng.R
import com.jinyi.wwdeng.databinding.ActPaPoBinding
import com.jinyi.wwdeng.pro.ProAct
import com.jinyi.wwdeng.utils.event.TrackEventType
import com.jinyi.wwdeng.utils.event.TrackEventUtils

class PaPoyAct : ProAct() {

    private lateinit var binding: ActPaPoBinding

    override fun getRootView(): View {
        binding = ActPaPoBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initView() {
        setTitleBarVisibility(View.VISIBLE)
        setTextTitle(getString(R.string.privacy_policy))
        setTextTitleGravity(Gravity.CENTER)
        setRightBtnRes(R.mipmap.pic_kefu_black_icon)

        initWebViewSettings()
    }

    override fun initListener() {
        //联系客服
        rootViewBinding.ivFun.setOnClickListener {
            onIntent(PaPoyAct::class.java)
            TrackEventUtils.logEvent(TrackEventType.CLICK.CLICK_CUSTOMER,"Privacy Policy Page")
        }
    }

    override fun initData() {
        binding.ppWebView.loadUrl(AppConfigs.PrivacyPolicyLink)
    }

    override fun onStart() {
        super.onStart()
        TrackEventUtils.logEvent(TrackEventType.PAGE.PAGE_PRIVACY_POLICY_START,"")
    }

    override fun onDestroy() {
        super.onDestroy()
        TrackEventUtils.logEvent(TrackEventType.PAGE.PAGE_PRIVACY_POLICY_END,"")
    }

    @SuppressLint("SetJavaScriptEnabled")
    private fun initWebViewSettings() {
        binding.ppWebView.setInitialScale(100)
        binding.ppWebView.webChromeClient = mWebChromeClient
        binding.ppWebView.webViewClient = mWebClient
        val webSettings = binding.ppWebView.settings
        webSettings.setSupportZoom(true)
        try {
            webSettings.javaScriptEnabled = true
            webSettings.textZoom = 100
            webSettings.domStorageEnabled = true
            webSettings.builtInZoomControls = true
            webSettings.useWideViewPort = true
            webSettings.loadWithOverviewMode = true
            webSettings.cacheMode = WebSettings.LOAD_NO_CACHE
            webSettings.layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN
            webSettings.mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
            webSettings.allowUniversalAccessFromFileURLs = true
            webSettings.allowFileAccessFromFileURLs = true
            webSettings.allowContentAccess = true
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    private val mWebChromeClient = object : WebChromeClient() {
        override fun onProgressChanged(view: WebView?, newProgress: Int) {
            super.onProgressChanged(view, newProgress)

            binding.progressBar.progress = newProgress
            if (newProgress == 100) {
                binding.progressBar.visibility = View.GONE
            } else {
                binding.progressBar.visibility = View.VISIBLE
            }
        }
    }

    private val mWebClient = object : WebViewClient() {
        override fun shouldOverrideUrlLoading(
            view: WebView?,
            request: WebResourceRequest?,
        ): Boolean {
            return shouldOverrideUrlLoading(view, request!!.url.toString())
        }

        override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
            return false
        }
    }
}